import inspect
import sys

from protopy import sub_rs_pb2


class StockCommand:
    # 订阅行情
    SC_STOCK_SUB = 101
    # 退订行情
    SC_STOCK_UN_SUB = 103

    # ********************* 主推 **********************#
    # 市场初始化推送
    S_MARKET_INIT = 1000
    # 市场时间推送
    S_MARKET_OVERVIEW = 1001
    # 行情快照推送
    S_STOCK_SNAPSHOT = 1002
    # 盘口推送
    S_STOCK_BID = 1003
    # 成交明细推送
    S_STOCK_TRANSACTION = 1004

    # ********************* 其他请求应答 **********************#
    # 服务端支持的市场
    SC_MARKET_SUPPORT = 2000
    # 市场概要
    SC_MARKET_OVERVIEW = 2001
    # 股票基本信息
    SC_STOCK_BASE_INFO = 2002
    # 股票快照
    SC_STOCK_SNAPSHOT = 2003
    # 盘口
    SC_STOCK_BID = 2004
    # 股票分时
    SC_STOCK_TREND = 2005
    # 股票K线
    SC_STOCK_KLINE = 2006
    # 股票成交明细
    SC_STOCK_TRANSACTION = 2007
    # 股票版块
    SC_STOCK_PLATE = 2008
    # 市场排序
    SC_MARKET_SORT = 2009
    # 分红派息
    SC_LAST_QX = 2013
    # K线指标
    SC_KLINE_INDEX=2014
    # 分时指标
    SC_TREND_INDEX=2015
    # 股票搜索
    SC_STOCK_SEARCH = 2030

    mappingJson = {
        "101": {
            "req": "sub_req_pb2",
            "res": "sub_rs_pb2"
        },
        "103": {
            "req": "un_sub_req_pb2",
            "res": "sub_rs_pb2"
        },
        "1000": {
            "res": "push_market_init_info_pb2"
        },
        "1001": {
            "res": "market_overview_pb2"
        },
        "1002": {
            "res": "stock_snapshot_pb2"
        },
        "1003": {
            "res": "bid_pb2"
        },
        "1004": {
            "res": "transaction_push_info_pb2"
        },
        "2000": {
            "req": "common_req_pb2",
            "res": "support_market_rs_pb2"
        },
        "2001": {
            "req": "market_overview_req_pb2",
            "res": "market_overview_rs_pb2"
        },
        "2002": {
            "req": "stock_base_info_req_pb2",
            "res": "stock_base_info_rs_pb2"
        },
        "2003": {
            "req": "stock_snapshot_req_pb2",
            "res": "stock_snapshot_rs_pb2"
        },
        "2004": {
            "req": "stock_bid_req_pb2",
            "res": "stock_bid_rs_pb2"
        },
        "2005": {
            "req": "stock_trend_req_pb2",
            "res": "stock_trend_rs_pb2"
        },
        "2006": {
            "req": "stock_kline_req_pb2",
            "res": "stock_kline_rs_pb2"
        },
        "2007": {
            "req": "stock_transaction_req_pb2",
            "res": "stock_transaction_rs_pb2"
        },
        "2008": {
            "req": "stock_plate_req_pb2",
            "res": "stock_plate_rs_pb2"
        },
        "2009": {
            "req": "market_sort_req",
            "res": "market_sort_rs"
        },
        "2013": {
            "req": "last_qx_req_pb2",
            "res": "last_qx_rs_pb2"
        },
        "2014":{
            "req":"kline_index_req_pb2",
            "res":"kline_index_rs_pb2"
        },
        "2015":{
            "req":"trend_index_req_pb2",
            "res":"trend_index_rs_pb2"
        },
        "2030": {
            "req": "stock_brief_req_pb2",
            "res": "stock_brief_rs_pb2"
        }
    }


def getResMessageByFunctionId(functionId, resOrReq=None):
    """
    根据functionId返回映射类
    :param functionId: 功能👌
    :param resOrReq:  "res"｜｜"req" 默认 "res"
    :return:
    """
    if resOrReq == None:
        resOrReq = 'res'

    value = StockCommand.mappingJson[functionId]
    resModuleStr = value[resOrReq]
    # 导入对应模块
    __import__('protopy.' + resModuleStr)
    # 获取模块
    resModule = sys.modules.get('protopy.' + resModuleStr)
    class_list = []

    for name, class_ in inspect.getmembers(resModule, inspect.isclass):
        class_list.append(class_)
    return class_list[0]
